<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */

// no direct access
defined( '_VALID_MOS' ) or die( 'Прямой доступ запрещен.' );

// Добавить новую категорию
if (isset($_POST['add_cat'])) {
	$new_cat_name = trim($_POST['new_cat_name']);
	if ($new_cat_name == '')
		mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_CATNONEME );
	$sql = ('INSERT INTO #__forum_categories (cat_name) VALUES(\''.$database->getEscaped($new_cat_name).'\')');
	$database->setQuery($sql);
	$database->Query($sql);
	mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_CATADDSUS );
}
// Удалить категорию
else if (isset($_POST['del_cat']) || isset($_POST['del_cat_comply'])) {

	$cat_to_delete = intval($_POST['cat_to_delete']);
	if ($cat_to_delete < 1)
		mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_404 );
	if (isset($_POST['del_cat_comply']))	// Удалить форум и все посты
	{
		@set_time_limit(0);
		$sql = ('SELECT id FROM #__forum_forums WHERE cat_id='.$cat_to_delete);
		$database->setQuery($sql);
		$database->query();
		$num_forums = $database->setQuery($sql)->getNumRows();

		for ($i = 0; $i < $num_forums; ++$i) {
			$cur_forum = $database->loadResult($sql, $i);
			// Prune all posts and topics
			//prune($cur_forum, 1, -1);
			// Удалиь форум
			$sql = ('DELETE FROM #__forum_forums WHERE id='.$cur_forum);
			$database->setQuery($sql);
			$database->Query($sql);
		}
		// Locate any "orphaned redirect topics" and delete them
		$sql = ('SELECT t1.id FROM #__forum_topics AS t1
						LEFT JOIN #__forum_topics AS t2 ON t1.moved_to=t2.id 
						WHERE t2.id IS NULL 
						AND t1.moved_to IS NOT NULL');
		$database->setQuery($sql);
		$database->query();
		$num_orphans = $database->getNumRows();

		if ($num_orphans) {
			for ($i = 0; $i < $num_orphans; ++$i)
				$orphans[] = $database->loadResult($sql, $i);
			$sql = ('DELETE FROM #__forum_topics WHERE id IN('.implode(',', $orphans).')');
			$database->setQuery($sql);
			$database->Query($sql);
		}
		// Delete the category
		$sql = ('DELETE FROM #__forum_categories WHERE id='.$cat_to_delete);
		$database->setQuery($sql);
		$database->Query($sql);
		// Regenerate the quickjump cache
		require_once 'components/com_forum/include/cache.php';
		generate_quickjump_cache();
		mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_CATDELSUS );
	}
	else	// If the user hasn't comfirmed the delete
	{
		$sql = ('SELECT cat_name FROM #__forum_categories WHERE id='.$cat_to_delete);
		$cat_name = $database->setQuery($sql)->loadResult();


		?>

<table class="adminheading">
	<tr>
		<th class="menus"> <span><?php echo _Y_CATDEL; ?></span> </th>
	</tr>
</table>
<table class="adminform">
	<tr>
		<th colspan="4"> <?php echo _Y_CATDELAVT; ?></th>
	</tr>
	<tr>
		<td><form method="post" action="<?php echo 'index2.php?option=com_forum&amp;task=categories'; ?>">
				<input type="hidden" name="cat_to_delete" value="<?php echo $cat_to_delete ?>" />
				<p><?php echo _Y_CATWARN; ?>&nbsp;[<?php echo $cat_name ?>] ?</p>
				<p><?php echo _Y_CATWARNING; ?></p>
				<p>
					<input type="submit" name="del_cat_comply" value="<?php echo _E_REMOVE; ?>" />
					<input onclick="javascript:history.go(-1)" name="" type="button" value="<?php echo _CMN_CANCEL; ?>" />
				</p>
			</form></td>
	</tr>
</table>
		<?php
	}
}
else if (isset($_POST['update']))	// Change position and name of the categories
{
	$cat_order = $_POST['cat_order'];
	$cat_name = $_POST['cat_name'];
	//s
	$database->setQuery('SELECT id, disp_position FROM #__forum_categories ORDER BY disp_position');
	$database->query();
	$num_cats = $database->getNumRows();
	$rows=$database->loadRowList();
	for ($i = 0; $i < $num_cats; ++$i) {
		if ($cat_name[$i] == '')
			mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_CATNONEME);
		if (!preg_match('#^\d+$#', $cat_order[$i]))
			mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_CATPOSITERR);
		list($cat_id, $position) = $rows[$i];

		$sql = ('UPDATE #__forum_categories SET cat_name=\''.$database->getEscaped($cat_name[$i]).'\', disp_position='.$cat_order[$i].' WHERE id='.$cat_id);
		$database->setQuery($sql);
		$database->Query($sql);
	}
	// Regenerate the quickjump cache
	require_once $yarbb_admin_path.'/include/cache.php';
	generate_quickjump_cache();
	mosRedirect( 'index2.php?option=com_forum&amp;task=categories', _Y_CATAPDATE);
}
// Generate an array with all categories
//s
$database->setQuery('SELECT id, cat_name, disp_position FROM #__forum_categories ORDER BY disp_position');
$database->query();
$num_cats = $database->getNumRows();
$rows=$database->loadRowList();
for ($i = 0; $i < $num_cats; ++$i)
	$cat_list[] = $rows[$i];


?>
<table class="adminheading">
	<tr>
		<th class="menus"> <span><?php echo _Y_CATADDEDDEL; ?></span> </th>
	</tr>
</table>
<form method="post" action="<?php echo 'index2.php?option=com_forum&amp;task=categories&amp;action=foo'; ?>">
	<table class="adminform">
		<tr>
			<th colspan="4"> <?php echo _Y_CATADDDELIT; ?></th>
		</tr>
		<tr>
			<td width="150px"><?php echo _Y_CATADDNEWC; ?></td>
			<td width="100px"><input type="submit" name="add_cat" value="<?php echo _E_ADD; ?>" tabindex="2" /></td>
			<td><input name="new_cat_name" type="text" tabindex="1" onblur="if(this.value=='') this.value='<?php echo _Y_CATADD_NAME; ?>';" onfocus="if(this.value=='<?php echo _Y_CATADD_NAME; ?>') this.value='';" value="<?php echo _Y_CATADD_NAME; ?>" size="35" maxlength="80" />
			</td>
		</tr>
<?php if ($num_cats): ?>
		<tr>
			<td><?php echo _Y_CATDEL; ?></td>
			<td><input type="submit" name="del_cat" value="<?php echo _E_REMOVE; ?>" tabindex="4" /></td>
			<td><select name="cat_to_delete" tabindex="3">
			<?php
	while (list(, list($cat_id, $cat_name, ,)) = @each($cat_list))
		echo "\t\t\t\t\t\t\t\t\t\t".'<option value="'.$cat_id.'">'.yarbb_htmlspecialchars($cat_name).'</option>'."\n";
	?>
				</select>
				<span><?php echo mosToolTip(_Y_CATDEL_DD); ?></span> </td>
		</tr>
					<?php endif; ?>
	</table>
	<p>
<?php if ($num_cats): ?>
	</p>
	<p>&nbsp; </p>
	<table class="adminlist">
		<tr>
			<th colspan="4"> <?php echo _Y_CATCURRENT; ?></th>
		</tr>
		<tr>
			<th width="10%"><b>ID</b></th>
			<th width="10%"><b><?php echo _WEBLINK_NAME; ?></b></th>
			<th width="10%"><b><?php echo _MODULE_POSITION; ?></b></th>
		</tr>
	<?php
	@reset($cat_list);
	for ($i = 0; $i < $num_cats; ++$i) {
		list(, list($cat_id, $cat_name, $position)) = @each($cat_list);
		?>
		<tr class="row0">
			<td><center>
				<?php echo $cat_id; ?>
				</center></td>
			<td><center>
					<input type="text" name="cat_name[<?php echo $i ?>]" value="<?php echo yarbb_htmlspecialchars($cat_name) ?>" size="35" maxlength="80" />
				</center></td>
			<td align="center"><input type="text" name="cat_order[<?php echo $i ?>]" value="<?php echo $position ?>" size="3" maxlength="3" />
			</td>
		</tr>
		<?php
	}
	?>
	</table>
	<table class="adminlist">
		<tr>
			<th colspan="4" align="right"><input type="submit" name="update" value="<?php echo _UPDATE; ?>" /></th>
		</tr>
	</table>
<?php endif; ?>
</form>